package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.core.enumeration.MenuType;
import com.ngc.mall.domain.MenuDomain;
import com.ngc.mall.domain.PermissionMenuDomain;
import com.ngc.mall.mapper.MenuMapper;
import com.ngc.mall.service.IMenuService;
import com.ngc.mall.service.IPermissionMenuService;
import com.ngc.mall.vo.MenuPermissionVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDomain> implements IMenuService {

    @Resource
    private IPermissionMenuService iPermissionMenuService;


    @Override
    public List<MenuDomain> selectAllMenu() {
//        return super.list(Wrappers.<MenuDomain>lambdaQuery().orderByAsc(MenuDomain::getSort));
        return super.list(Wrappers.<MenuDomain>lambdaQuery().orderByDesc(MenuDomain::getCreateTime));
    }
    @Override
    public Long createMenu(MenuDomain menuDomain){
        return super.create(menuDomain);
    }

    @Override
    public List<MenuPermissionVO> selectAllMenuWithPermissions() {
//        List<MenuDomain> menuDomainList=super.list();
//        List<MenuPermissionVO> menuPermissionVOList=new ArrayList<>();
//        for(MenuDomain menuDomain:menuDomainList){
//            Long id=menuDomain.getId();
//            List<PermissionMenuDomain> permissionMenuDomainList=iPermissionMenuService.list(Wrappers.<PermissionMenuDomain>lambdaQuery().eq(PermissionMenuDomain::getMenuId,id));
//            List<PermissionDomain> permissionList=new ArrayList<>();
//            for(PermissionMenuDomain permissionMenuDomain:permissionMenuDomainList){
//                Long permissionId=permissionMenuDomain.getPermissionId();
//                PermissionDomain permissionDomain=iPermissionService.getOne(Wrappers.<PermissionDomain>lambdaQuery().eq(PermissionDomain::getId,permissionId));
//                permissionList.add(permissionDomain);
//            }
//            MenuPermissionVO menuPermissionVO=MenuPermissionVO.builder().id(menuDomain.getId()).pid(menuDomain.getPid()).title(menuDomain.getTitle()).permissionList(permissionList).build();
//            menuPermissionVOList.add(menuPermissionVO);
//        }
//
//       return menuPermissionVOList;
        return null;
    }

    @Override
    public Boolean updateStatusById(Long id, boolean status) {
        return null;
    }

    @Override
    public List<MenuDomain> getMenusByPermissions(List<Long> permissions) {
        List<MenuDomain> menuDomainList=new ArrayList<>();
        for(Long permissionId:permissions){
            PermissionMenuDomain permissionMenuDomain=iPermissionMenuService.getOne(Wrappers.<PermissionMenuDomain>lambdaQuery().eq(PermissionMenuDomain::getPermissionId,permissionId));
            MenuDomain menuDomain=super.getById(permissionMenuDomain.getMenuId());
            if(!menuDomainList.contains(menuDomain)){
                menuDomainList.add(menuDomain);
            }
        }
        return menuDomainList;
    }

    @Override
    public List<MenuDomain> findMenuIsShow() {
        return super.list(Wrappers.<MenuDomain>lambdaQuery().eq(MenuDomain::getIsShow,true));
    }

    @Override
    public Boolean updateById(Long id, MenuDomain menuDomain) {
        return super.update(menuDomain,Wrappers.<MenuDomain>lambdaUpdate().eq(MenuDomain::getId,id));
    }

    @Override
    public List<MenuDomain> findMenuByMySelf(List<Long> roleIdList) {
     List<MenuDomain> newMenuList=new ArrayList<>();
     List<MenuDomain> originMenuList= baseMapper.findMenusByRoleIdList(roleIdList);
     newMenuList.addAll(originMenuList);
        for(MenuDomain domain:originMenuList){
            String permission=domain.getPermission();
            if(domain.getType()!=MenuType.FOLDER.getValue()){
                String[] permissions= permission.split(":");
                String[] pathList = Arrays.copyOfRange(permissions, 0, permissions.length - 1);
                List<String> newArr=new ArrayList<>(Arrays.asList(pathList));
                if(newArr.isEmpty()){
                    continue;
                }
                List<MenuDomain> pMenuList= this.findPMenuListByRouterName(originMenuList,newArr);
                newMenuList.addAll(pMenuList);
            }

        }
        ///去重
        newMenuList=newMenuList.stream().distinct().collect(Collectors.toList());
        return newMenuList;
    }

    ///根据截取的path和routerName做对比
    private List<MenuDomain> findPMenuListByRouterName(List<MenuDomain> menuList,List<String> pathList){
        List<MenuDomain> pMenuList=new ArrayList<>();
        for(String iPath:pathList){
            Optional<MenuDomain> pMenuDomainOptional= Optional.ofNullable(this.findMenu(menuList,iPath));
            if(!pMenuDomainOptional.isPresent()){
                MenuDomain pMenuDomain=super.getOne(Wrappers.<MenuDomain>lambdaQuery().eq(MenuDomain::getRouterName,iPath));
                if(pMenuDomain!=null){
                    pMenuList.add(pMenuDomain);
                }

            }
        }
        return pMenuList;
    }

    private MenuDomain findMenu(List<MenuDomain> menuList,String path){
        for(MenuDomain domain:menuList){
            if(domain.getPath()==path){
                return domain;
            }
        }
        return null;
    }


}
